//
//  UISlider+YHExtension.swift
//  Adorbee
//
//  Created by amovision on 2024/12/11.
//

import UIKit

public extension UISlider {
  convenience init(_ value: Float = 0, min: Float = 0, max: Float, tColor: UIColor, minColor: UIColor, maxColor: UIColor) {
    self.init(frame: .zero)
    self.value = value
    minimumValue = min
    maximumValue = max
    thumbTintColor = tColor
    minimumTrackTintColor = minColor
    maximumTrackTintColor = maxColor
  }

  func addClickAction(_ action: @escaping (UISlider, Float) -> Void) {
    let tap = UITapGestureRecognizer { sender in
      let tp = sender as! UITapGestureRecognizer
      let sl = tp.view as! UISlider
      let p = tp.location(in: tp.view)
      let value = (sl.maximumValue - sl.minimumValue) * Float((p.x / sl.frame.size.width))
      action(self, value)
    }
    addGestureRecognizer(tap)
    addHandler(for: .touchDown) { _ in
      tap.isEnabled = false
    }
    addHandler(for: [.touchUpInside, .touchUpOutside]) { _ in
      tap.isEnabled = true
    }
  }
}
